const express = require('express');
const Tools = require('../modules/Tools');
const Logger = require('../modules/Logger');
const Authorize = require('../modules/Authorize');

const router = express.Router();

/* GET users listing. */
router.get('/', Authorize, (req, res) => {
    const { username } = req.session;
    res.render('index', { title: `用户 ${username}` });
});

/* GET 用户登录 */
router.get('/login', (req, res) => {
    Tools.delSession(req, 'username');
    res.setHeader('Keep-alive', '1');
    res.render('login');
});
/* POST 用户登录 */
router.post('/login', (req, res) => {
    const { username, password } = req.body;
    if (username === password) {
        // 登陆成功后保存session即可
        Tools.setSession(req, 'username', username);
        Logger.info('用户登陆成功');
        res.redirect('/users');
    } else res.end('fail');
});

module.exports = router;
